<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <!-- 编辑时才显示-->
                  <a-form-model-item label="物资招标计划编码">
                    <BaseInput
                      v-model="formData.tenderCode"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="工程名称"
                    prop="wbsId"
                  >
                    <a-select
                      v-model="formData.wbsId"
                      :disabled="state === 3"
                    >
                      <a-select-option
                        v-for="item in getProgrammeArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="起始时间"
                    prop="startDate"
                  >
                    <a-date-picker
                      v-model="formData.startDate"
                      value-format="YYYY-MM-DD"
                      type="date"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="截止时间"
                    prop="endDate"
                  >
                    <a-date-picker
                      v-model="formData.endDate"
                      value-format="YYYY-MM-DD"
                      :disabled-date="disabledDate"
                      type="date"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item
                    label="项目名称"
                    prop="projectId"
                  >
                    <SelectDownProject
                      ref="downProject"
                      v-model="formData.projectId"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                v-if="state !== 3"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="物资招标计划明细表">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <detail
              :id="row ? row.id : null"
              ref="detail"
              :extra-ref="$refs.downProject"
              :state="state"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[2]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :upload-list="planAddFile"
              :delete-url="planFileListDel"
              :is-show="state !== 3"
              :list-url="planFileList"
              :business-id="row.id"
              business-code="MM02"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <div
        v-if="!isAudit"
        class="pub-header-btn"
      >
        <a-button
          v-if="state !== 3"
          :loading="loading"
          type="primary"
          @click="submit"
        >
          提交
        </a-button>
        <BasePrintButton
          v-if="formData.id && state == 3"
          :before-print-fn="beforePrintFn"
          style="display: inline-block"
        />
        <a-button
          v-checkVisibleInDetail
          @click="$close"
        >
          关闭
        </a-button>
      </div>
    </base-breadcrumb>
  </div>
</template>

<script>
import moment from 'moment'
import detail from '../components/detail/detail'
import { selectOption } from '@/api/material/goods/goods'
import {
    planListAdd, planListEdit, planAudit,
    planAddFile, planFileList, planFileListDel, planListDetail,print
} from '@/api/material/procurement/plan'
import { themeChoose } from '@/mixin/mixin'
import SelectDownProject from '@/components/Select/SelectDownProject'

export default {
    name:'a'+Date.now(),
    mixins: [themeChoose],
    components: {
        detail,
        SelectDownProject
    },
    props: {
        isAudit: { // 是否作为审批详情
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            beforePrintFn: async () => {
                let res = await print({ id: this.formData.id })
                // res.data = res.data.replaceAll('\n', '\\')
                return res.data.content
            },
            activeKey: [true, false, false, false, false, false, false],
            loading: false,
            state: Number(this.$route.query.state),
            isSubmit: false,
            row: this.$route.query.row,
            formData: {
                createTime: this.getNowTime(),
                endDate: null,
                startDate: null,
                wbsId: null,
                tenderCode: null,
                creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
                projectId: ''
            },
            rules: {
                wbsId: { required: true, message: '请选择工程名称', trigger: 'change' },
                startDate: { required: true, message: '请选择起始时间', trigger: 'change' },
                endDate: { required: true, message: '请选择截止时间', trigger: 'change' },
                projectId: { required: true, message: '请选择项目名称', trigger: 'change' }
            },
            stepVal: 0,
            isNext: false,
            planAddFile: planAddFile(),
            planFileList: planFileList(),
            planFileListDel: planFileListDel(),
            getProgrammeArr: []
        }
    },
    watch: {
        isAudit: { // 是否作为审批详情
            immediate: true,
            handler(val) {
                if (val) {
                    this.state = 3
                }
            }
        },
        stepVal(newVal, oldVal) {
            this.$set(this.activeKey, newVal, true)
        }
    },
    mounted: function() {
        this.getProgramme()
        if (this.state !== 1) {
            this.getEditInfo()
            this.isSubmit = true
        }
    },
    methods: {
        disabledDate(current) {
            if (this.formData.startDate) {
                let startDate
                if (typeof this.formData.startDate === 'string') {
                    startDate = moment(this.formData.startDate)
                } else {
                    startDate = this.formData.startDate
                }
                return current && current < startDate.endOf('day')
            }
            return true
        },
        previous: function() {
            this.isNext = true
            this.stepVal--
            if (this.stepVal < 0) {
                this.stepVal = 0
            }
        },
        getProgramme: function() {
            let _this = this
            selectOption({
                type: 18
            }).then(res => {
                if (res.code === 200) {
                    _this.getProgrammeArr = res.data
                }
            })
        },
        next: function(val) {
            this.isNext = true
            this.stepVal++
            if (this.stepVal >= 3) {
              this.stepVal = 3
            }
        },
        stepChange: function(val) {
            console.log(val)
            if (!this.row && !this.isAudit) {
                if (val) {
                    this.$message.warning('切换步骤前请先填写基本信息')
                    this.stepVal = 0
                }
            } else {
                if (!this.isNext) {
                    this.$set(this.activeKey, val, !this.activeKey[val])
                }
            }
            this.isNext = false
        },
        // 获取编辑回显数据
        async getEditInfo() {
            const params = {
                id: this.$route.query.id || this.row.id
            }
            this.loading = true
            const res = await planListDetail(params)
            if (res.code === 200) {
                this.row = res.data || {}
                this.formData.id = res.data.id
                this.formData.endDate = this.row.endDate
                this.formData.startDate = this.row.startDate
                this.formData.wbsId = this.row.wbsId
                this.formData.tenderCode = this.row.tenderCode
                this.formData.createTime = this.row.createTime
                this.formData.creatorName = this.row.creatorName
                this.formData.projectId = this.row.projectId
            }
            this.loading = false
        },
        submit() {
            // this.save('approve');
            if (this.isSubmit) {
                this.$submitConfirm(async () => {
                    this.loading = true
                    let res = await planAudit({ baseId: this.row.id, auditStatus: this.row.auditStatus })
                    if (res.code === this.SUCCESS_CODE) {
                        this.$message.success('提交成功')
                        this.loading = false
                        this.$router.push({ path: '/material/procurement/plan' })
                    }
                    this.loading = false
                })
            } else {
                this.$message.warning('提交之前请先暂存')
            }
        },
        temporaryStorage(isNext) {
            if (this.state === 3) {
                this.next()
                return
            }
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    if (this.state === 2 || this.row) {
                        this.formData.id = this.row.id
                        planListEdit(this.formData).then(res => {
                            if (res.code === 200) {
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    } else {
                        planListAdd(this.formData).then(res => {
                            if (res.code === 200) {
                                this.row = res.data
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                this.getEditInfo()
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    }
                } else {
                  this.$message.warning("请填写完所有必填项信息")
                    return false
                }
            })
        }
    }
}
</script>
